//实现 点击订单明细页的 返回购物车按钮 返回购物车页面
$(".order-detail .title .right-part span").on("click", function () {
    window.location.href = "gwc.html";
});

//订单明细页面使用数据
for (let i = 0; i < localStorage.length; i++) {
    let keyName = localStorage.key(i);
    if (keyName.indexOf("_pay") !== -1) {
        let valuesMap = JSON.parse(localStorage.getItem(keyName));
        let pay_goods_img_path = valuesMap[0].pay_goods_img_path;
        let pay_goods_name = valuesMap[1].pay_goods_name;
        let pay_goods_color = valuesMap[2].pay_goods_color;
        let pay_goods_amount = valuesMap[3].pay_goods_amount;
        let pay_goods_totalPrice = valuesMap[4].pay_goods_totalPrice;
        let liEle = `<li>
                        <img src="` + pay_goods_img_path + `" alt="">
                        <div class="goods-detail">
                            <span class="goods-name">` + pay_goods_name + `</span>
                            <span class="goods-color">颜色分类: <span>` + pay_goods_color + `</span></span>
                            <span>数量: <span>` + pay_goods_amount + `</span></span>
                        </div>
                        <div class="price">
                            <span>` + pay_goods_totalPrice + `</span>
                        </div>
                    </li>`;
        let ulEle = $(".order-block ul");
        ulEle.append(liEle);
    }
}

$(function () {
    //实现用户地址的删除
    $(".address .right-part span:first-child").on("click", function () {
        $(this).parent().parent().parent().addClass("active");
        let divList = $(".address>div");
        for (let i = 0; i < divList.length; i++) {
            console.log(divList[i].className);
            if ($(divList[i]).hasClass("active")) {
                divList.eq(i).remove();
            }
        }
    });

    //实现获取订单内容中所有商品的金额总和
    let totalPrice = 0;
    let priceList = $(".order-block ul li");
    for (let i = 0; i < priceList.length; i++) {
        totalPrice += Number($(priceList[i]).children(".price").children().html().replace("￥", ""));
    }
    $(".order-content .total-price span:last-child").html("￥" + Number(totalPrice).toFixed(2));

    //实现用户地址栏的点击效果
    let pointAddr = null;
    $(".addr-child").on("click", function () {
        if (pointAddr !== null) {
            pointAddr.removeClass("activeAddr");
            pointAddr.css("border", "1px solid #DBDBDB");
        }
        $(this).css("border", "1px solid #A10000");
        $(this).addClass("activeAddr");
        pointAddr = $(this);
    });

    //实现地址栏新增地址
    $(".shopping-order .user-address .title span:last-child").on("click", function () {
        $(".mask").css("display", "block");
        $(".add-address").css("display", "block");
    });

    //实现地址栏编辑地址
    let edit_object;
    $(".shopping-order .addr-child .right-part span:last-child").on("click", function () {
        edit_object = $(this);
        $(".mask").css("display", "block");
        $(".edit-address").css("display", "block");
    });

    //实现新增地址保存功能
    $(".add-address .save-addr-btn button:first-child").on("click", function () {
        let consignee_name = $(".consignee-name").val();
        let cconsignee_addr = $(".consignee-addr").val();
        let consignee_phone = $(".consignee-phone").val();
        let divEle = $(".shopping-order .address");
        let addr_child = `<div class="addr-child">
                            <div class="more-information">
                                <div class="left-part">
                                    <span>`+ consignee_name + `</span>
                                    <span>[默认地址]</span>
                                </div>
                                <div class="right-part">
                                    <span>删除</span>
                                    <span>|</span>
                                    <span>编辑</span>
                                </div>
                            </div>
                            <div class="real-addr">
                                <span>` + cconsignee_addr + `</span>
                            </div>
                            <div class="phoneNmuber">
                                <span>` + consignee_phone + `</span>
                            </div>
                        </div>`;
        divEle.append(addr_child);
        $(".mask").css("display", "none");
        $(".add-address").css("display", "none");
    });

    //实现新增地址取消功能
    $(".add-address .save-addr-btn button:last-child").on("click", function () {
        $(".mask").css("display", "none");
        $(".add-address").css("display", "none");
    });

    //实现编辑地址保存功能
    $(".edit-address .save-addr-btn button:first-child").on("click", function () {
        let consignee_name = $(".consignee-name").val();
        let cconsignee_addr = $(".consignee-addr").val();
        let consignee_phone = $(".consignee-phone").val();
        $(edit_object)
        $(".mask").css("display", "none");
        $(".add-address").css("display", "none");
    });

    //实现编辑址取消功能
    $(".edit-address .save-addr-btn button:last-child").on("click", function () {
        $(".mask").css("display", "none");
        $(".edit-address").css("display", "none");
    });
});